package org.iabako.server.dao.business;

import org.iabako.server.dao.JpaDAO;
import org.iabako.server.tools.ServerTools;
import org.iabako.shared.entity.business.Tracking;
import org.iabako.shared.entity.business.TrackingObject;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.entity.user.User;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by jose on 04/05/15.
 */

@Repository("trackingDAO")
public class TrackingDAO extends JpaDAO<Long, Tracking> {

    private List<Tracking> byUserSession;

    @Override
    protected String getUniqueNumberVerificationQuery() {
        return null;
    }

    @Override
    protected String getLastNumberQuery() {
        return null;
    }

    public Tracking getByTrackingTypeAndSessionUser(TrackingType trackingType) {
        ServerTools serverTools = new ServerTools();
        Date today = serverTools.resetTime(new Date());
        Date tomorrow = serverTools.addDaysToDate(today, 1);

        String query = "SELECT t FROM Tracking t " +
                "WHERE t.type = :type " +
                "AND t.date >= :today AND t.date < :tomorrow " +
                "AND t.user = :user";

        List<Tracking> trackingList = entityManager.createQuery(query)
                .setParameter("type", trackingType)
                .setParameter("today", today)
                .setParameter("tomorrow", tomorrow)
                .setParameter("user", getSessionUser()).getResultList();

        return trackingList == null || trackingList.isEmpty() ? null : trackingList.get(0);
    }

    public List<Tracking> getTodayTracking() {
        ServerTools serverTools = new ServerTools();
        Date today = serverTools.resetTime(new Date());
        Date tomorrow = serverTools.addDaysToDate(today, 1);

        String query = "SELECT t FROM Tracking t join t.user.enterprise e " +
                "WHERE t.date >= :today AND t.date < :tomorrow " +
                "AND e IN (:enterprises) " +
                "ORDER BY t.date asc";
        return entityManager.createQuery(query)
                .setParameter("today", today)
                .setParameter("tomorrow", tomorrow)
                .setParameter("enterprises", getSessionUser().getEnterprise().getVisibleEnterprisesFinancialData()).getResultList();
    }

    public void persistTrackingObject(TrackingObject trackingObject) {
        entityManager.persist(trackingObject);
    }

    public List<TrackingObject> getTrackingObject(Tracking tracking) {
        String query = "SELECT to FROM TrackingObject to WHERE to.tracking = :tracking";
        return entityManager.createQuery(query).setParameter("tracking", tracking).getResultList();
    }

    public List<Tracking> getTrackingsOn(Date date) {
        ServerTools serverTools = new ServerTools();
        date = serverTools.resetTime((Date) date.clone());
        Date datePlusOne = serverTools.addDaysToDate((Date) date.clone(), 1);

        String query = "SELECT t FROM Tracking t " +
                "WHERE t.date >= :date AND t.date < :datePlusOne";

        return entityManager.createQuery(query).setParameter("date", date).setParameter("datePlusOne", datePlusOne).getResultList();
    }

    public List<Tracking> getTrackingByDateAndUsers(Date date, List<User> users) {
        ServerTools serverTools = new ServerTools();
        date = serverTools.resetTime((Date) date.clone());
        Date datePlusOne = serverTools.addDaysToDate((Date) date.clone(), 1);

        String query = "SELECT t FROM Tracking t WHERE t.user IN (:users) AND t.date >= :date AND t.date < :datePlusOne";
        return entityManager.createQuery(query)
                .setParameter("date", date)
                .setParameter("datePlusOne", datePlusOne)
                .setParameter("users", users).getResultList();
    }
}
